package DMSXL._2024.BinaryTrees;

import com.sun.source.tree.Tree;

import java.util.LinkedList;
import java.util.Queue;

public class invertTree {
    public static void main(String[] args) {
        Integer[] nums = {1,2,3,4,5,6,7};
        TreeNode root = CreateTree.createBinaryTree(nums,0);
        TreeNode newroot = new invertTree().invertTree(root);
        preorderTraversal preorderTraversal = new preorderTraversal();
        System.out.println(preorderTraversal.preorderTraversal(newroot));
    }
    public TreeNode invertTree(TreeNode root){
        if(root == null){
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int k = queue.size();
            for (int i = 0; i < k; i++) {
                TreeNode cur = queue.poll();
                TreeNode temp = cur.left;
                cur.left = cur.right;
                cur.right = temp;
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
        }
        return root;
    }
}
